<template>
  <div class="topic">
      <div class="topic_left">
        <div class="topic_leftBtn">
          <a href="/article">看文章</a>
          <a href="/User/topics/uploadTopics" target="_blank">聊话题</a>
        </div>
        <topics-ranking v-if="timeRanking.length" :rankingData="timeRanking.slice(0,10)">
          <svg class='icon' aria-hidden='true'>
            <use xlink:href='#icon-yishoucang'></use>
          </svg>今日话题
        </topics-ranking>
        <topics-ranking v-if="hostRanking.length" :rankingData="hostRanking.slice(0,10)">
          <svg class='icon' aria-hidden='true'>
            <use xlink:href='#icon-remen'></use>
          </svg>热门话题
        </topics-ranking>
      </div>
      <div class="topic_right">
        <div class="nav">
          <div class="tag">
            <span :class="{'active':this.classify==0}" @click="init">全部话题</span>
            <span :class="{'active':this.classify==1}" @click="hostTopics">热门话题</span>
          </div>
          <serach @seach="seachTopics"></serach>
        </div>
        <ul v-if="isTopics">
          <li v-for="(item,index) in result" :key="index">
            <a href="JavaScript:;" @click="toTopics(item.topicsid)" class="topics_box">
              <div class="info">
                <div class="title">
                  <svg class='icon' aria-hidden='true'>
                    <use xlink:href='#icon-chaojihuati'></use>
                  </svg>
                  <h4>{{item.title}}</h4>
                </div>
                <span class="text">{{item.content}} 
                  <span style="font-size:14px;color:gray;">{{item.filetype | fileType}}</span>
                </span>
              </div>
              <div class="portrait">
                <img :src="item.picture">
                <span>{{item.name}}</span>
                <div class="box">
                  <span>
                    <svg class='icon' aria-hidden='true'>
                      <use xlink:href='#icon-shijian'></use>
                    </svg>
                    <span>{{timer(item.time)}}</span>
                  </span>
                  <span>
                    <svg class='icon' aria-hidden='true'>
                      <use xlink:href='#icon-youlanrenshu'></use>
                    </svg>
                    <span>{{item.visit}}</span>
                  </span>
                  <span>
                    <svg class='icon' aria-hidden='true'>
                      <use xlink:href='#icon-pinglun-copy'></use>
                    </svg>
                    <span>{{item.commentCount}}</span>
                  </span>
                </div>
              </div>
            </a>
          </li>
        </ul>
        <div class="not_data" v-else>
          <svg class='icon' aria-hidden='true'>
            <use xlink:href='#icon-baoqian'></use>
          </svg>
          <span>抱歉,找不到相关话题...</span>
        </div>
        <div class="paging">
          <button @click="paging--" :disabled="paging==1">《</button>
          <span>{{paging}}</span>
          <button @click="paging++" :disabled="pagingSum">》</button>
        </div>
      </div>
  </div>
</template>

<script>
import tool from '../../assets/js/tool'
import Serach from '../info/serach.vue'
import TopicsRanking from '../topics/topicsRanking.vue'
export default {
  name: 'All',

  data() {
    return {
      resultList: [], // 所有话题数据集
      result: [], // 展示话题数据集
      hostRanking: [], // 热门话题数据集
      timeRanking:[], // 今日话题数据集
      paging: 1, // 当前页数
      count: 4, // 每页数量
      classify: 0 // 分类
    };
  },

  computed: {
    // 返回时间格式
    timer(){
      return (time) => tool.GetTimer(time)
    },
    // 最大页数
    pagingSum(){
      return this.paging == parseInt(this.resultList.length/this.count) + 1
    },
    // 判断是否有话题
    isTopics(){
      return this.result.length != 0
    }
  },

  filters: {
    fileType(type){
      switch(type){
        case 'video': return '[视频]'
        case 'image': return '[图片]'
        default: return ''
      }
    }
  },
  
  watch: {
    paging(val){
       this.result = this.resultList.slice((val-1)*this.count,val*this.count)
    }
  },

  mounted() {
    this.init()
  },

  methods: {
    // 初始化 
    init(){
      this.classify = 0
      this.paging = 1
      this.$api.getTopics().then(res => {
        this.resultList = res.data
        this.hostRanking = res.data.slice(0).sort((a,b) => b.visit - a.visit)
        this.timeRanking = res.data.filter((v) => 
          v.time.split('T')[0] == new Date( +new Date() + 8 * 3600 * 1000 ).toJSON().slice(0,10).replace("T"," "))
        this.result = this.resultList.slice(0,this.count)
      }).catch(() => {
        this.$store.dispatch('showHint', {
          isFlag: false,
          title: '加载失败,稍后重试!'
        });
      })
    },
    // 热门话题
    hostTopics(){
      this.classify = 1
      this.resultList.sort((a,b) => {
        return b.visit - a.visit
      })
      this.paging = 1
      this.result = this.resultList.slice(0,this.count)
    },
    // 检索话题
    seachTopics(val){
      this.$api.getTopics().then(res => {
        this.resultList = res.data.filter((a) => {
          return a.title.indexOf(val) != -1
        })
        this.paging = 1
        this.result = this.resultList.slice(0,this.count)
      })
    },
    // 跳转话题 
    toTopics(id){
      this.$router.push('/topic/' + id)
    }
  },

  components: {
    TopicsRanking,Serach
  },
};
</script>

<style scoped>
.topic{
  display: flex;
  width: 100%;
  margin: 0px auto;
  padding: 20px;
  background-color: #ececec;
}
.topic .topic_left{
  flex: 1;
  font-size: 0.7rem;
}
.topic .topic_left .topic_leftBtn{
  width: 100%;
  height: 60px;
  background-color: white;
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}
.topic_leftBtn a {
  display: inline-block;
  width: 80px;
  height: 30px;
  line-height: 30px;
  border: 1px solid gray;
  text-align: center;
  margin: 0 20px;
}
.topic_leftBtn a:hover,.topic_leftBtn a:nth-child(2){
  background-color: green;
  color: white;
}
.topic .topic_right{
  flex: 4;
  margin-left:40px;
  background-color: white;
}
.topic .topic_right .nav{
  display: flex;
  justify-content: space-between;
  width: 100%;
  padding: 10px;
  border-bottom: 1px solid rgb(185, 185, 185);
}
.topic .topic_right .nav .tag{
  display: flex;
  flex-wrap: wrap;
}
.topic_right .nav span{
  display: inline-block;
  padding: 6px 10px;
  font-size: 0.7rem;
  cursor: pointer;
  margin-right: 10px;
  border: 1px solid gray;
}
.topic_right .nav .active{
  background-color: green;
  color: white;
  transition: all .3s;
}
.topic_right li{
  padding: 20px 30px 4px 30px;
  border-bottom: .5px solid rgb(216, 216, 216);
}
.topic_right .topics_box{
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
}
.topic_right .portrait{
  display: flex;
  align-items: center;
  font-size: 0.8rem;
  color: gray;
}
.topic_right .portrait img{
  width: 30px;
  height: 30px;
}
.topic_right .info{
  margin: 10px 0;
}
.topic_right .info .text{
  display: inline-block;
  font-size: 0.8rem;
  margin: 10px 0;
  text-indent: 2em;
}
.topic_right .info .title{
  display: flex;
}
.topic_right .info .title .icon{
  font-size: 1.5em;
}
.topic_right .box{
  font-size: 0.5rem;
  color: gray;
  display: flex;
  margin-left: 10px;
}
.topic_right .box .icon{
  font-size: 1rem;
}
.topic_right .box > span{
  padding: 5px;
  background-color: rgb(206, 206, 206);
  margin: 0 4px;
  color: gray;
  display: flex;
  align-items: center;
}
.topic_right .paging{
  display: flex;
  justify-content: flex-end;
  padding: 20px;
}
.topic_right .paging button{
  display: inline-block;
  width: 25px;
  height: 30px;
  line-height: 30px;
  cursor: pointer;
  background-color: white;
  border: 1px solid gray;
  font-size: 0.5rem;
  text-align: center;
}
.topic_right .paging span{
  text-align: center;
  font-size: 0.5rem;
  line-height: 30px;
  width: 20px;
  background-color: green;
  color: white;
}
.topic_right .not_data{
  font-size: 0.7rem;
  color: gray;
  text-align: center;
  padding: 30px;
}
.topic_right .not_data .icon{
  font-size: 2rem;
}
@media screen and (max-width:800px) {
  .topic{
    padding: 0;
    flex-direction: column-reverse;
  }
  .topic .topic_left .topic_leftBtn{
    display: none;
  }
  .topic .topic_right{
    margin: 0;
  }
}
</style>